package browse

import (
	"black_mamba/pages"
	_ "embed"
	"encoding/json"
	"fmt"
	"gitee.com/wengo/windows_gui/widget"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

type BrowsePage struct {
	pages.Foundation
	style        map[string]int
	taskTotal    *widget.ShapeText
	detailLayout *widget.LayoutEle
}

func NewBrowsePage() *BrowsePage {
	browsePage := &BrowsePage{
		Foundation: pages.Foundation{
			ExecuteBtn: widget.NewButtonByName("browseExecuteBtn"),
			LogEdit:    widget.NewEditByName("browseLogEdit"),
			Style:      nil,
			Topic:      "browse",
		},
	}

	// 绑定执行按钮事件
	browsePage.ExecuteBtn.Event_BnClick(browsePage.execute)

	// 设置log样式
	browsePage.SetEditStyle()

	//绑定所有订阅
	browsePage.Subscribes()
	return browsePage
}

/* 事件回调 */
func (b *BrowsePage) execute(pbHandled *bool) int {
	*pbHandled = true

	go func() {
		path, _ := launcher.LookPath()
		u := launcher.New().
			Bin(path).
			Headless(false).
			Set("start-maximized").
			Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36").
			Set("disable-blink-features", "AutomationControlled").
			MustLaunch()
		browser := rod.New().ControlURL(u).MustConnect()
		defer browser.MustClose()

		//cookies := `[{"Name":"i18n-prefs","Value":"USD","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:35:59Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"session-id","Value":"139-0683573-4014714","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:36:03Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"session-id-time","Value":"2288878563l","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:36:03Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"skin","Value":"noskin","Path":"\/","Domain":"amazon.com","Expires":"9999-12-31T23:59:59Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"ubid-main","Value":"135-7655844-2351025","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:36:03Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"session-token","Value":"H75nw74bQ7d2gewlTDI1ek07zGueMBEvXZXzAnoUvbiKyRPeyvb9F5BEr4OsO7XLuSvOB9uu649BpId2WEG6JeXIP1MkS\/LrCHgyA3bATlIlWtqsjERda3vhNgrQjSCTUZlRW6vO5P\/j8LhTHi4UoK4PPtZ4W2If1GOhvAcW4ttkonddDxvI3Jyygxoy8M65us0NOauOVq2w\/tS4\/qDV+WTKJ\/Bb0lYiAP1O9TC167SBzZwad1Tf847CRt+wmi3F","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:36:03Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"at-main","Value":"Atza|IwEBIOxIXCRNSLEcqZKT_LhYwDIRJ1FAekDxqQGpZSk3IaOIMM1dsJc90ENKuJS9ui_6pSnq-a8Ui-LQDOuE0uLmZUWk6jHJnuf1NFBVrYxB7M4VD8Rumod5yz_e7qDkhHpSGGW7ZY7FnDeKkPiYuu8CHu2x9ub_Ss5yQqTNI9omS-cDFjP-7oh4F80iIesOyALVJ2kUWWgvAqdF0ot0H61ZCRln","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:35:58Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":true,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"lc-main","Value":"en_US","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:35:58Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"sess-at-main","Value":"j\/mFMEaK5rVCtpx59Nb7wvjhTccqauhXho3r2W6Z4eM=","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T23:35:59.0904097+08:00","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":true,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"sst-main","Value":"Sst1|PQFu7QyTVUZG7c3h_VrOu6RsCWwrZHFYDdtVQVLx6pRDMXLPICu7DSy2F84PtPezJRbdRmKbTyanX699AgaDIIJfWcW_LZyuEnIBE6ti7E25hu0XacphEkrjy_BvHyntVqripAyZNxzAJ1I6Ux3aDI7CED89DWynH2Ri5TDgCZTu6jYlqjvrWVVEoVeL98iRN9Z_I3cv43Jjx87uot3uZ3K13GmhIzZklMEFLNHSSxjLthZ4EzevLFcnm685AhFVMxjGrmc6OK9Gi-MaOY_9IeM87BOtlsRUhxwZKbdrBbtKGhE","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T15:35:58Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":true,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"x-main","Value":"XdXnTYaRais8IOPA5?vTxU8r16nwdoOtTMzlgU0VvYfy1gezaoWqzCFkUp@PMA42","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T23:35:59.5218411+08:00","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":false,"SameSite":0,"Raw":"","Unparsed":null},{"Name":"sp-cdn","Value":"L5Z9:RU","Path":"\/","Domain":"amazon.com","Expires":"2023-07-18T23:35:59.5218411+08:00","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":true,"SameSite":0,"Raw":"","Unparsed":null}]`
		cookies := `[{"domain":".amazon.com","httpOnly":true,"name":"at-main","path":"\/","secure":true,"value":"Atza|IwEBIIEN59RW02y8ptX_cF62A_m0ieurA3tPd2-CGJOm89rHBQBa6etLqLV-bDT4TrJZ_zuvTONl3LkjWwew-8ZTgJg7DhFk_1LCWIW8LiHk40nHfehHkt9XhCw_rho7i_PpQBC8UbMqFxXyCyQw1ppgC7ryrj60dsG05avhVaf39gTlytSKLuyxQQ36nKeWqpFNvWgstgdUVQ0wSLouwQ7ZZjTc"},{"domain":".amazon.com","httpOnly":false,"name":"i18n-prefs","path":"\/","secure":false,"value":"USD"},{"domain":".amazon.com","httpOnly":false,"name":"lc-main","path":"\/","secure":false,"value":"zh_CN"},{"domain":".amazon.com","httpOnly":true,"name":"sess-at-main","path":"\/","secure":true,"value":"\"oCAMwnksS4U9DKF\/z6XbjUkmUUpSH3gbJ9fJepeB0rQ=\""},{"domain":".amazon.com","httpOnly":false,"name":"session-id","path":"\/","secure":true,"value":"143-2296674-0948361"},{"domain":".amazon.com","httpOnly":false,"name":"session-id-time","path":"\/","secure":false,"value":"2082787201l"},{"domain":".amazon.com","httpOnly":false,"name":"session-token","path":"\/","secure":true,"value":"\"9vmJ3bCSWh+195eeucIZaRteTFoU8CvwzPW82ExBETctzyTzSxaoFTrYjmCrIBd6Xz+GZ1AZhB8DtoNgt5Td\/i8qsyAZojgUYjSKg992tmSKuo6aXJ9C6HN\/QFaqsJ82\/+HizQoMq6NtrFC3MHpgljkEJLMcms+a7UD7Iqj\/zNz+OtzItTSkV8mZjmy2nLGrTJomY17rkp8pc\/4SlSrmTBzPBt2hMbQryps6JkLJqStwrwG+dsWz5C4pihtroAGSTWwkXmqM7AbIGePUPSvlAweZeChWrXr2I6\/Q60wkp0A=\""},{"domain":".amazon.com","httpOnly":false,"name":"skin","path":"\/","secure":false,"value":"noskin"},{"domain":".amazon.com","httpOnly":true,"name":"sp-cdn","path":"\/","secure":true,"value":"\"L5Z9:CN\""},{"domain":".amazon.com","httpOnly":true,"name":"sst-main","path":"\/","secure":true,"value":"Sst1|PQGyiVqgQ9JhJ0DqhWpcgvozCUGpoBM5OqHs7kaJb2ELWPSJzmFcS7eBfFZR1GR_gIR3Bk7HIN-1gLk8s3ry47pLxEjb-zqgk8yZrjY_2SnXc7zTQo7XtzH-_H-aS9Wp3WPkk1Gdhbrm_ihmkbPKjrcok0w1n1ZJaIiWXC38WeliR40mudO-BkTUVlkQoeuzNbbd7lUApP_wYxysO8gRqE1Rju1qySzEr3a0XPIwBSN30kg5c9cxSGSHhw0dv-O4KeEh7rqRa7Nc284Mc0qcZWHrgYxwAA60rGHPEf96g0W3xO0"},{"domain":".amazon.com","httpOnly":false,"name":"ubid-main","path":"\/","secure":true,"value":"130-5190388-7649757"},{"domain":".amazon.com","httpOnly":false,"name":"x-main","path":"\/","secure":true,"value":"\"Y0YMrPl70AdYCEIjdba2g4MGE?sUH4ZKZNBv5OSaVLm@GsCW0aw7lLRh3NZxAntl\""},{"domain":"www.amazon.com","httpOnly":false,"name":"csm-hit","path":"\/","secure":false,"value":"tb:s-PVZNTA170W4X85KWNMQ2|1656733205045&t:1656733205896&adb:adblk_no"}]`
		var c []*proto.NetworkCookie
		json.Unmarshal([]byte(cookies), &c)
		_, err := rod.New().ControlURL(u).MustConnect().MustSetCookies(c...).Page(proto.TargetCreateTarget{URL: "https://www.amazon.com/"})
		if err != nil {
			fmt.Println(err)
		}

		time.Sleep(100 * time.Minute)

		fmt.Println("ojbk")
	}()

	return 0
}
